package org.libra {
	import flash.events.Event;
	import flash.events.MouseEvent;
	import org.libra.basic.Component;
	import org.libra.basic.Container;
	import org.libra.geom.IntPoint;

	/**
	 * ...
	 * @author zhangzuoqiang
	 */
	public class JContextMenu extends JPanel {
		
		protected var menuItems:Vector.<JMenuItem>;
		private var curMenuItem:JMenuItem;
		
		public function JContextMenu(owner:Container) { 
			super(owner);
			this.setMasked(false);
			this.setCloseable(false);
			menuItems = new Vector.<JMenuItem>();
		}
		
		override public function append(c:Component):Boolean {
			if (c is JMenuItem) {
				throw new Error("JMenuItem can't be append to,you should user appendItem()");
				return false;
			}
			return super.append(c);
		}
		
		override public function remove(c:Component, removeOwner:Boolean = true):Boolean { 
			if (c is JMenuItem) {
				throw new Error("JMenuItem can't be remove to,you should user removeItem()");
				return false;
			}
			return super.remove(c, removeOwner);
		}
		
		override public function show():void {
			super.show();
			for each(var item:JMenuItem in menuItems) {
				item.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
			}
			this.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
		}
		
		override public function tryToClose():void {
			super.tryToClose();
			for each(var item:JMenuItem in menuItems) {
				item.removeEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
			}
			this.removeEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
		}
		
		private function onRollOutHandler(e:MouseEvent):void {
			removeChildPanel();
		}
		
		private function onRollOverHandler(e:MouseEvent):void {
			removeChildPanel();
			var childPanel:Container = null;
			curMenuItem = e.target as JMenuItem;
			childPanel = curMenuItem.getChildPanel();
			if (childPanel) {
				this.append(childPanel);
				if (childPanel.getY() + this.getY() + childPanel.getHeight() < stage.stageHeight) {
					childPanel.setLocation(new IntPoint(curMenuItem.getX() + curMenuItem.getWidth(), curMenuItem.getY()));
				}else {
					childPanel.setLocation(new IntPoint(curMenuItem.getX() + curMenuItem.getWidth(), stage.stageHeight - this.getY() - childPanel.getHeight()));
				}
			}
		}
		
		private function removeChildPanel():void {
			if (curMenuItem) {
				var childPanel:Container = curMenuItem.getChildPanel();
				if (childPanel) {
					this.remove(childPanel);
				}
			}
		}
		
		public function appendItem(item:JMenuItem):void {
			if (menuItems.indexOf(item) == -1) {
				var y:int = 0;
				for each(var i:JMenuItem in menuItems) {
					y += i.getHeight();
				}
				item.setLocation(new IntPoint(0, y));
				this.menuItems[menuItems.length] = item;
				super.append(item);
			}
		}
		
		public function appendAllItems(...items):void {
			for each(var item:JMenuItem in items) {
				this.appendItem(item);
			}
		}
		
		public function removeItem(item:JMenuItem):void {
			var index:int = menuItems.indexOf(item);
			if (index != -1) {
				this.menuItems.splice(index, 1);
				super.remove(item);
			}
		}
		
		public function clearItem():void {
			this.menuItems.length = 0;
			this.removeAll();
		}
		
		override public function dispose():void {
			super.dispose();
			for each(var item:JMenuItem in menuItems) {
				item.dispose();
			}
		}
		
		override public function toString():String {
			return "JContextMenu";
		}
	}

}